package com.cdty.chess.controller;

import java.util.List;
import java.util.Map;

import com.cdty.chess.service.FileService;
import com.cdty.chess.util.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import lombok.RequiredArgsConstructor;

/**
 * 文件控制器
 */
@RestController
@RequestMapping("/api/v1/files")
@RequiredArgsConstructor
public class FileController {

    @Autowired
    private final FileService fileService;
    
    /**
     * 批量上传文件
     *
     * @param files 文件列表
     * @return 文件URL列表
     */
    @PostMapping("/batch")
    @ResponseStatus(HttpStatus.CREATED)
    public Map<String, List<String>> uploadFiles(@RequestParam("files") List<MultipartFile> files) {
        var urls = fileService.uploadFiles(files);
        return Map.of("urls", urls);
    }
    
    /**
     * 批量删除文件
     *
     * @param fileUrls 文件URL列表
     * @return 操作结果
     */
    @DeleteMapping("/batch")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteFiles(@RequestBody List<String> fileUrls) {

        fileService.deleteFiles(fileUrls);
    }
    
    /**
     * 列出指定目录下的所有文件和子目录
     *
     * @param directory 目录路径，为空则列出根目录
     * @return 文件和目录列表
     */
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public FileUtils.CosFileList listFiles(@RequestParam(required = false) String directory) {
        return fileService.listFiles(directory);
    }
    
    /**
     * 创建目录
     *
     * @param directory 目录路径
     */
    @PostMapping("/directory")
    @ResponseStatus(HttpStatus.CREATED)
    public void createDirectory(@RequestParam String directory) {
        fileService.createDirectory(directory);
    }
}